package com.zhdl.modules.filter;

import com.project.common.util.DelayUtil;
import com.zhdl.hardware.io.smartLink.service.IOCmdSendService;
import com.zhdl.hardware.motor.huichuan.service.HCCmdSendService;
import com.zhdl.hardware_modules.service.impl.DeviceServiceImpl;
import com.zhdl.hardware_modules.web.response.ProcessStepResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Component
public class FilterProcess_New {

    /**
     * 压滤流程-测试模式， 流程进行
     */
    public boolean isTest = false, isRunning = false;
    /**
     * 压滤流程进度
     */
    public int processMotor = 0, sensor01Count = 0, lastSensor01Status = 0;
    List<ProcessStepResp> processStepResps;
    private final String ioNum = "30";
    @Resource
    private IOCmdSendService ioCmdSendService;
    @Resource
    private HCCmdSendService hcCmdSendService;

    /**
     * 流程开始初始化
     */
    public void init() {

        processStepResps = DeviceServiceImpl.processStepResp;

        ioCmdSendService.readAllCoil02(ioNum, bytes -> {
            /* IO 0 到位信号 4 流程开始的标记 */
            Byte io4 = bytes.get(4);
            if (!isRunning) {
                if (io4 == 1) {
                    hcCmdSendService.motorMoveDistance("8", "01", 8000, 200, s -> {
                        log.info("相机拍照-拍一张右侧");
                    });
                    isRunning = true;
                }
                log.info("当前流程并非正在运行++++" + io4);
            }


            Byte io0 = bytes.get(0);
            if (io0 == 1) {
                if (lastSensor01Status == 0) {
                    sensor01Count++;
                    lastSensor01Status = 1;
                    if (sensor01Count % 2 == 0) {
                        nextIntMotor();
                    }
                }
            } else {
                lastSensor01Status = 0;
            }
        });
    }

    /**
     * 电机流程_压滤
     */
    public void nextIntMotor() {

        if (processMotor >= 43) {
            return;
        }
        processMotor++;

        if (processMotor == 43) {
            hcCmdSendService.motorMoveDistance("7", "01", 0, 600, s -> {
                log.info("电机回原点");
                DelayUtil.delayedExecution(1000, () -> {
                    processMotor = 0;
                    isRunning = false;
                });
            });
        } else {
            DelayUtil.delayedExecution(1000, new Runnable() {
                @Override
                public void run() {
                    hcCmdSendService.motorMoveDistance("8", "01", 4000, 200, s -> {
                        //拍正中间照片>>>
                        log.info("拍正中间照片>>>>>>>>>>>>>>>>>>>>>>>");
                        hcCmdSendService.motorMoveDistance("7", "01", (-160000) * processMotor, 200, s22 -> {

                            DelayUtil.delayedExecution(1000, new Runnable() {
                                @Override
                                public void run() {
                                    hcCmdSendService.motorMoveDistance("8", "01", 0, 200, s1 -> {
                                        //拍左侧照片>>>
                                        log.info("相机拍照-电机移动-左 >>>>>>>>>>>>>>>>>>>>>>>");
                                        log.info("相机拍照-拍照左");
                                        DelayUtil.delayedExecution(1000, new Runnable() {
                                            @Override
                                            public void run() {
                                                //电机到位以后 先左边拍照 然后 右边拍照
                                                hcCmdSendService.motorMoveDistance("8", "01", 8000, 200, s11 -> {
                                                    //拍左侧照片>>>
                                                    log.info("相机拍照-电机移动-右 >>>>>>>>>>>>>>>>>>>>>>>");
                                                });
                                                //电机到位以后 先左边拍照 然后 右边拍照
                                                log.info("相机拍照-拍照右");
                                            }
                                        });

                                    });
                                }
                            });

                        });

                    });
                }
            });
        }

    }
}
